package com.axx.android.moments;

import android.app.Activity;
import android.content.Context;
import android.support.annotation.LayoutRes;
import android.support.annotation.Nullable;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.axx.android.R;
import com.axx.android.profile.message.model.ChangeEvent;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.previewlibrary.PhotoActivity;
import com.previewlibrary.ThumbViewInfo;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * Created by sunjie on 2017/8/10.
 */

public class ShowMeAdapter extends BaseQuickAdapter<ShowMeModel.ShowMeListBean, BaseViewHolder> {

    public ShowMeAdapter(@LayoutRes int layoutResId, @Nullable List<ShowMeModel.ShowMeListBean> data) {
        super(layoutResId, data);
    }

    @Override
    protected void convert(BaseViewHolder helper, ShowMeModel.ShowMeListBean item) {
        helper.setText(R.id.tv_content, item.getText());
        if (item.getPlace().equals("不使用地址")){
            helper.setVisible(R.id.layout_location,false);
        }else{
            helper.setText(R.id.tv_location, item.getPlace());
            helper.setVisible(R.id.layout_location,true);
        }

        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            Date time = simpleDateFormat.parse(item.getTime());
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(time);
            int month = calendar.get(Calendar.MONTH);
            int day = calendar.get(Calendar.DAY_OF_MONTH);

            helper.setText(R.id.tv_month, month + 1 + "月");
            helper.setText(R.id.tv_day, day + "");

        } catch (ParseException e) {
            e.printStackTrace();
        }

        RecyclerView recyclerView = helper.getView(R.id.rv_image);
        if (item.getPic() != null && item.getPic().size() != 0) {
            final List<ImageModel> list = new ArrayList<>();
            final ArrayList<ThumbViewInfo> thumbViewInfos = new ArrayList<>();
            for (String s : item.getPic()) {
                ImageModel imageModel = new ImageModel();
                imageModel.setAdd(false);
                imageModel.setCanDel(false);
                imageModel.setUrl(s);
                list.add(imageModel);

                ThumbViewInfo thumbViewInfo = new ThumbViewInfo(s);
                thumbViewInfos.add(thumbViewInfo);
            }
            recyclerView.setVisibility(View.VISIBLE);
            ImageAdapter imageAdapter = new ImageAdapter(R.layout.view_item_image, list);
            recyclerView.setLayoutManager(new GridLayoutManager(mContext, 3));
            recyclerView.setAdapter(imageAdapter);
            final Context context = mContext;
            imageAdapter.setOnItemChildClickListener(new OnItemChildClickListener() {
                @Override
                public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                    PhotoActivity.startActivity((Activity) context,thumbViewInfos,position);

                }
            });
        } else {
            recyclerView.setVisibility(View.GONE);
        }


    }
}
